<template>
  <div>
    <el-container>
      <el-header>
        <img class="mlogo" src="../assets/logo.png" alt="logo图片" />
      </el-header>
      <el-main>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户姓名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="用户密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="zhuye">主页</el-button>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button type="danger" @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
    name: "Login",
    data () {
      return {
        ruleForm: {
          username: 'admin',
          password: '111111'
        },
        rules: {
          username: [
            {
              required: true, message: '请输入账户名称', trigger: 'blur'
            },
            {
              min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'
            }
          ],
          password: [
            {
            required: true, message: '请输入账户密码', trigger: 'blur'
            },
            {
              min: 6, message: '密码长度至少6位', trigger: 'blur'
              // min: 8, message: '必须包含字母大小写、数字三种类型，且长度大于8', trigger: 'blur'
            }
          ]
        }
      };
    },
    methods: {
      submitForm (formName) {
        this.$refs[formName].validate((valid) => {
          if(valid) {
            console.log("校验成功");
            alert('ok');
            // this.$axios.post("/login", this.ruleForm).then(res => {
            this.$axios.post("http://localhost:8081/login", this.ruleForm).then(res => {
              const jwt = res.headers['authorization'];
              const userInfo = res.data.data;

              console.log(userInfo);
              // 把数据共享出去
              this.$store.commit("SET_TOKEN", jwt);
              this.$store.commit("SET_USERINFO", userInfo);
              this.$router.push('/blogs');
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm (formName) {
        this.$refs[formName].resetFields();
      },
      zhuye () {
        this.$router.push("/");
      }
    }
}
</script>

<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-main {
  /* background-color: #e9eef3; */
  color: #333;
  text-align: center;
  line-height: 160px;
}
.mlogo {
  height: 80%;
}

.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}
</style>
